package com.baidu.ueditor.util;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 七牛云工具类<br>
 * 1.上传文件<br>
 * @author qiaowenbin
 * @version 0.0.2.20150803
 * @history
 * 	0.0.2.20150803<br>
 * 	0.0.1.20141209<br>
 */
public class QQiNiuUtil {
	
	public static final String ak = QPropertiesUtil.config.getProperty("qiniu.ak");
	public static final String sk = QPropertiesUtil.config.getProperty("qiniu.sk");
	public static final String bucket = QPropertiesUtil.config.getProperty("qiniu.bucket");
	public static final String domain = QPropertiesUtil.config.getProperty("qiniu.domain");
	public static String uptoken = null;
	
	/**
	 * 获取token
	 * @return
	 */
	public static String getUptoken(){
		return Auth.create(ak, sk).uploadToken(bucket);
	}
	
	/**
	 * 生成uptoken
	 */
	public static void genUptoken(){
		ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
		executor.scheduleAtFixedRate(new Runnable() {
			public void run() {
				uptoken = getUptoken();
			}
		}, 0, 3500, TimeUnit.SECONDS);
	}
	
	/**
	 * 上传文件
	 * @param path
	 * @param file
	 */
	
	public static void uploadFile(String path, String file){
        try {
        	Configuration cfg = new Configuration(Zone.zone0());
        	//...其他参数参考类注释
        	UploadManager uploadManager = new UploadManager(cfg);
			uploadManager.put(file, path, Auth.create(ak, sk).uploadToken(bucket));
		} catch (QiniuException e) {
			e.printStackTrace();
		}
	}
	
	
	public static String uploadFile1(String path, String file){
		/*String hou = path.substring(path.lastIndexOf("."), path.length());
		String qian = getPingYin(path.substring(0, path.lastIndexOf(".")));
		path = qian+hou;*/
		/*System.out.println(path);*/
		String aa = domain;
        try {
        	Configuration cfg = new Configuration(Zone.zone0());
        	//...其他参数参考类注释
        	UploadManager uploadManager = new UploadManager(cfg);
        	 com.qiniu.http.Response response =uploadManager.put(file, path, Auth.create(ak, sk).uploadToken(bucket));
        	 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        	 System.out.println(putRet.key);
        	 System.out.println(putRet.hash);
        	 aa+=putRet.key;
		} catch (QiniuException e) {
			e.printStackTrace();
		}
        return aa;
	}
	
	//返回一个拼音字符串，并且首字母大写
    public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0].substring(0, 1).toUpperCase() + t2[0].substring(1);
                    ;
                } else {
                    t4 += java.lang.Character.toString(t1[i]);
                }
            }


        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
       return t4;

    }
	
	// 将字符串转成hash值
		public static int toHash(String key) {
			int arraySize = 11113; // 数组大小一般取质数
			int hashCode = 0;
			for (int i = 0; i < key.length(); i++) { // 从字符串的左边开始计算
				int letterValue = key.charAt(i) - 96;// 将获取到的字符串转换成数字，比如a的码值是97，则97-96=1
														// 就代表a的值，同理b=2；
				hashCode = ((hashCode << 5) + letterValue) % arraySize;// 防止编码溢出，对每步结果都进行取模运算
			}
			return hashCode;
		}

	
	public static void main(String[] args) {

		System.out.println(uploadFile1("1521654652微信图片_20190131110602.jpg","C:\\Users\\艾洪凯\\Desktop\\电影博物馆\\微信图片_20190131110602.jpg"));

		/*System.out.println(uploadFile1("微信图片_20190131114801.jpg","C:\\Users\\Administrator\\Desktop\\微信图片_20190131114801.jpg"));*/

		System.out.println(domain);
		//FlYuluaeWjCOxLBYlUnhkeVGSlxL
	}
	
	
	
	
}
